using System;
using System.Collections.Generic;
using System.Linq;

namespace RAD.LightOne
{
    #region Documentation
    /// <summary>
    /// Define a uma classe a tabela no banco de dados ao qual ela se relaciona.
    /// </summary>
    #endregion
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple=true)]
    public sealed class DBTableAttribute : Attribute
    {
        #region Fields
        private string tableName;

        #endregion Fields

        #region Properties
        public string TableName 
        {
            get 
            {
                return tableName;
            }
        }

        #endregion Properties

        #region Constructors

        public DBTableAttribute(string tableName)
        {
            this.tableName = tableName;
        }
        #endregion Constructors

        #region Static Methods
        public static bool IsDefined(Type type)
        {
            return type.GetCustomAttributes(typeof(DBTableAttribute), false).Length > 0;
        }

        public static DBTableAttribute GetDefinedAttribute(Type type)
        {
            object[] attributes = type.GetCustomAttributes(typeof(DBTableAttribute), false);

            if (attributes.Length == 0)
            {
                return null;
            }
            else
            {
                return attributes[0] as DBTableAttribute;
            }

        }

        #endregion Static Methods
    }
}
